package com.yexiong.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.CheckDetail;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.yexiong.dao.ICheckDetailDao;

public class CheckDetailDaoImpl extends BaseDao implements ICheckDetailDao{

	@Override
	public int insert(CheckDetail checkDetail) {
		int result=0;
		String sql="insert into checkdetails(check_id,check_start_time,check_end_time,content) values(?,?,?,?)";
		Object[] objects= {checkDetail.getDetailCheckId(),checkDetail.getCheckDetailStartTime(),checkDetail.getCheckDetailEndTime(),checkDetail.getContent()};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public Object getEntity(ResultSet rs) {
		CheckDetail checkDetail=new CheckDetail();
		try {
				checkDetail.setCheckDetailId(rs.getInt("id"));
				checkDetail.setDetailCheckId(rs.getInt("check_id"));
				checkDetail.setCheckDetailStartTime(rs.getTimestamp("check_start_time"));
				checkDetail.setCheckDetailEndTime(rs.getTimestamp("check_end_time"));
				checkDetail.setContent(rs.getString("content"));
				
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return checkDetail;
	}

	@Override
	public int delete(String date,int check_id) {
		int result=0;
		String sql="delete from checkdetails where check_start_time like ? and check_id=?";
		Object[] objects= {"%"+date+"%",check_id};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public int update(CheckDetail checkDetail) {
		int result=0;
		String sql="update checkdetails set check_start_time=?,check_end_time=?,content=? where id=?";
		Object[] objects= {
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(checkDetail.getCheckDetailStartTime()),
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(checkDetail.getCheckDetailEndTime()),
				checkDetail.getContent(),
				checkDetail.getCheckDetailId()};
		result=excuteUpdate(sql, objects);
		return result;
	}

	@Override
	public int getId(int id, String date) {
		String sql="select * from checkdetails where check_id=? and check_start_time=?";
		Object[] objects= {id,date};
		List<CheckDetail> list=extuteQuery(sql, objects);
		return list.get(0).getCheckDetailId();
	}

	@Override
	public List<CheckDetail> selectTime(int checkId) {
		String sql="select * from checkdetails where check_id=?";
		List<CheckDetail> list=extuteQuery(sql, checkId);
		
		return list;
	}

	@Override
	public Date getFirstStartDate(int checkId) {
		String sql="select * from checkdetails where check_id=? order by check_start_time";
		List<CheckDetail> list=extuteQuery(sql, checkId);
		
		return list.get(0).getCheckDetailStartTime();
	}

	@Override
	public Date getLastEndDate(int checkId) {
		String sql="select * from checkdetails where check_id=? order by check_end_time desc";
		List<CheckDetail> list=extuteQuery(sql, checkId);
		
		return list.get(0).getCheckDetailEndTime();
	}

	
}
